﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;

namespace QLBN
{
    class TTCN
    {
        DBConnector dbc = new DBConnector();

        private String _username;

        public String Username
        {
            get { return _username; }
            set { _username = value; }
        }

        private String _pass;

        public String Pass
        {
            get { return _pass; }
            set { _pass = value; }
        }

        public TTCN()
        {
 
        }

        public TTCN(String username)
        {
            this._username = username;
        }

        public Boolean Kiem_Tra_Cap_Nhat(String username, String oldpass,String newpass)
        {
            Boolean ck = false;
            String sql = "Select Pass from NguoiDung where UserName ='" + _username + "'";
            DataSet ds = dbc.executeQuery(sql);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0][0].ToString() != oldpass)
                {
                    MessageBox.Show("Nhập sai mật khẩu!");
                }
                else
                {
                    sql = "Select UserName from NguoiDung";
                    OleDbDataReader reader = dbc.executeReader(sql);
                    while (reader.Read())
                    {
                        if (Comparer.Equals(reader["UserName"].ToString(), username))
                        {
                            if (username != _username)
                            {
                                MessageBox.Show("Không thể sử dụng tên đăng nhập này !");
                                ck = false;
                                return false;

                            }
                        }
                        ck = true;
                    }
                }
            }
            if (ck == true)
            {
                sql = "Update NguoiDung set UserName='" + username + "' where UserName='" + _username + "'";
                dbc.executeNonQuery(sql);
                _username = username;
                //MessageBox.Show(_username, "TTCN");
                //update Account set Pass = '" & TextBox2.Text & "' where UserID ='" & Passedempid & "'"

                return true;
            }
            else
            {
                return false;
            }
            return false;
        }
    }
}
